package cn.a592n.weightlibs;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.a592n.weightlibs.weight.MPullView;

public class PullDownActivity extends AppCompatActivity {

    @InjectView(R.id.mp)
    MPullView mp;
    @InjectView(R.id.main_view)
    RelativeLayout mainView;

    private float maxSize = 400;
    private float oldY = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_down);
        ButterKnife.inject(this);

        mainView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {


                switch (motionEvent.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        oldY = motionEvent.getY();
//                        Log.d("PROGRESS_IO", "oldY= " + oldY);
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        float y = motionEvent.getY();
                        if (y > oldY) {
                            //向下滑动
                            float size = y - oldY;
                            float progress = size > maxSize ? 1 : size / maxSize;
//                            Log.d("PROGRESS_IO", "oldY= " + oldY + "Y= " + y + "  progress= " + progress + "");
                            if (!mp.isRefreashing) {
                                mp.setmProgress(progress,true);
                            }
                        } else {
                            float size = y - oldY;
                            float progress = size > maxSize ? 1 : size / maxSize;
//                            Log.d("PROGRESS_IO", "oldY= " + oldY + "Y= " + y + "  progress= " + progress + "");
                            if (mp.isRefreashing) {
                                mp.setmProgress(progress,false);
                            }
                        }

                        return true;
                    case MotionEvent.ACTION_UP:
                        mp.startRefeash();
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

    }

    @OnClick(R.id.btn_refreash)
    public void onViewClicked() {
        mp.setRefreshFinish(true);
    }
}
